package org.janzen_zhang.androidftp;

import android.provider.SyncStateContract;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Created by Janzen on 16-7-15.
 */
public class PropertiesUtil {

    /**
     * 读Properties
     * @param fileName
     * @param key
     * @return
     */
    public static String get(String fileName, String key) {
        Properties properties = new Properties();
        try {
            InputStream in = new FileInputStream(fileName);
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties.getProperty(key);
    }

    /**
     * 写Properties
     * @param fileName
     * @param key
     * @param value
     */
    public static void put(String fileName, String key, String value) {
        Properties properties = new Properties();
        try {
            InputStream in = new FileInputStream(fileName);
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        properties.setProperty(key, value);
        OutputStream fos;
        try {
            fos = new FileOutputStream(fileName);
            properties.store(fos, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /*private static File getSettingFile() throws IOException {
        File setting = new File(path);
        if(!setting.exists()) setting.createNewFile();
        return setting;
    }*/
}
